package com.hxx.common.core.wcontour.read;

import com.hxx.common.core.wcontour.base.TrainData;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MappingJsonFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 网格Json文件读取
 *
 * @author hxd
 */
public class GridJsonUtil {

    /**
     * 读取气象json文件
     * 返回PAEAMS是以“1321”开头（标识经度线有1321条），后面跟着有1321个值，对应经度的值；接着是“1031”开头（标识纬度线有1031条），后面跟着有1031个值，对应纬度的值；（再后面就是720
     * 个超大数据，暂时不知道是什么数据，先不理会）然后1321*1031组成一个网格，共有1361951个经纬度，而1361951刚好等于DATA数组的数量
     *
     * @param jsonFile json文件路径
     * @return 网格数据对象
     */
    public static TrainData read(File jsonFile) throws IOException {
        JsonFactory f = new MappingJsonFactory();
        JsonParser jp = f.createParser(jsonFile);
        JsonToken current;
        current = jp.nextToken();
        if (current != JsonToken.START_OBJECT) {
            return null;
        }
        List<Double> data = new ArrayList<>();
        List<Double> params = new ArrayList<>();
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jp.getCurrentName();
            // move from field name to field value
            current = jp.nextToken();
            if ("DATA".equals(fieldName)) {
                if (current == JsonToken.START_ARRAY) {
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        JsonNode node = jp.readValueAsTree();
                        data.add(node.asDouble());
                    }
                } else {
                    jp.skipChildren();
                }
            } else if ("PARAMS".equals(fieldName)) {
                if (current == JsonToken.START_ARRAY) {
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        JsonNode node = jp.readValueAsTree();
                        params.add(node.asDouble());
                    }
                } else {
                    jp.skipChildren();
                }
            } else {
                jp.skipChildren();
            }
        }
        int lonSize = params.get(0).intValue();
        int latSize = params.get(lonSize + 1).intValue();
        List<Double> lonList = params.subList(1, lonSize + 1);
        List<Double> latList = params.subList(lonSize + 2, lonSize + 2 + latSize);
        TrainData trainData = new TrainData();

        double[] dataArray = new double[data.size()];
        for (int i = 0; i < data.size(); i++) {
            dataArray[i] = data.get(i);
        }
        double[] xArray = new double[lonList.size()];
        for (int i = 0; i < lonList.size(); i++) {
            xArray[i] = lonList.get(i);
        }
        double[] yArray = new double[latList.size()];
        for (int i = 0; i < latList.size(); i++) {
            yArray[i] = latList.get(i);
        }
        double[][] matrix = new double[latSize][lonSize];
        int k = 0;
        for (int i = 0; i < yArray.length; i++) {
            for (int j = 0; j < xArray.length; j++) {
                matrix[i][j] = dataArray[k];
                k++;
            }
        }
        trainData.set_X(xArray);
        trainData.set_Y(yArray);
        trainData.setData(matrix);
        trainData.setSize(new int[]{xArray.length, yArray.length});
        return trainData;
    }

}
